package com.jlect.swebing.ui.client;

import com.jlect.swebing.renderers.client.*;

/**
 * Checkbox component
 *
 * @author Sergey Kozmin
 * @since 19.11.2007 10:19:36
 */
public class GCheckbox extends GComponent implements RendererActionListener {
    private boolean checked;

    private ActionListenerCollection clickListeners;

    public GCheckbox(String text) {
        setText(text);
    }

    public GCheckbox() {
        this("");
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
        getCheckboxRenderer().setChecked(checked);
    }

    public void setText(String text) {
        getCheckboxRenderer().setText(text);
    }

    public void addActionListener(ActionListener listener) {
        getClickListener().add(listener);
    }

    public void removeActionListener(ActionListener listener) {
        if (clickListeners != null) {
            getClickListener().remove(listener);
        }
    }

    protected CheckboxRenderer getCheckboxRenderer() {
        return (CheckboxRenderer) getRenderer();
    }

    private ActionListenerCollection getClickListener() {
        if (clickListeners == null) {
            clickListeners = new ActionListenerCollection();
        }
        return clickListeners;
    }

    public void fireActionPerformed(RendererEvent event) {
        getClickListener().clickActionPerformed(this);
    }

    protected Renderer createRenderer() {
        CheckboxRenderer checkboxRenderer = (CheckboxRenderer) RenderersConfig.getRenderersFactory().createComponentRenderer(DefaultRenderers.CHECKBOX.getName());
        checkboxRenderer.setActionListener(this);
        return checkboxRenderer;
    }
}
